#include <iostream>
#include <vector>
#include "managerservice_impl.h"
#include "manager.h"
#include "tools.h"

using namespace std;

vector<Manager*> managers;
ManagerServiceImpl::ManagerServiceImpl(void)
{
	dao = new ManagerDaoFileImpl;
}

bool ManagerServiceImpl::addManager(Manager& mem)
{
	dao->load(managers);
	managers.push_back(&mem);

	dao->save(managers);

	return true;
}

bool ManagerServiceImpl::deleteManager(int _id)
{
	managers.clear();
	dao->load(managers);

	if(!managers.size()) return false;

	vector<Manager*>::iterator itor;	
    for (itor = managers.begin(); itor != managers.end(); itor++)
    {
        if((*itor)->get_id() == _id)
        {       
            itor = managers.erase(itor);
            dao->save(managers);
            return true;
        }
       
	}

	return false;
}

/*ostream& operator << (ostream& os,vector<Manager*>& managers)
{
	for(unsigned int i=0; i<managers.size(); i++)
	{
		cout<<managers.at(i)->get_id()<<endl;
		getchar();
		getchar();
	}
	return os;
}
*/
void ManagerServiceImpl::listManager(void)
{
	dao->load(managers);

	for(unsigned int i=0; i<managers.size(); i++)
	{
		cout<<managers.at(i)->get_id()<< " "<<managers.at(i)->get_name()<<" "<< managers.at(i)->get_passwd()<<endl;
	}

}
ManagerServiceImpl::~ManagerServiceImpl(void)
{
	delete []dao;
}
